package frontend;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import concrete.Player;



//VS4E -- DO NOT REMOVE THIS LINE!
public class InJail extends JDialog {

	
	private Player player;
	private BoardFrame boardFrame;
	private JButton rollDoubleButton;
	private JButton postBailButton;
	private JButton useCardButton;
	private JLabel backgroundImage;
	private static final long serialVersionUID = 1L;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
	//CONSTRUCTORS
	public InJail() {
		initComponents();
	}
	public InJail(BoardFrame parent) {
		super(parent, "In Jail", true);
		this.boardFrame = parent;
		this.player = parent.getCurrentPlayer();
		initComponents();
	}

	public BoardFrame getBoardFrame() {
		return this.boardFrame;
	}
	public Player getPlayer() {
		return this.player;
	}
	
	public void setBoardFrame(BoardFrame boardFrame) {
		this.boardFrame = boardFrame;
	}
	public void setPlayer(Player player) {
		this.player = player;
	}

	private void initComponents() {
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setLocation(175,210);
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(Color.white);
		setResizable(false);
		setForeground(Color.black);
		setLayout(new GroupLayout());
		add(initRollDoubleButton(), new Constraints(new Leading(310, 10, 10), new Leading(268, 10, 10)));
		add(initPostBailButton(), new Constraints(new Leading(217, 92, 10, 10), new Leading(268, 6, 6)));
		add(initUseCardButton(), new Constraints(new Leading(125, 92, 10, 10), new Leading(268, 6, 6)));
		add(initBackgroundImage(), new Constraints(new Leading(0, 6, 6), new Leading(0, 301, 6, 6)));
		addWindowListener(new WindowAdapter() {
	
			public void windowClosing(WindowEvent event) {
			}
		});
		setSize(415, 330);
	}
	private JLabel initBackgroundImage() {
		if (backgroundImage == null) {
			backgroundImage = new JLabel();
			backgroundImage.setIcon(new ImageIcon(getClass().getResource("/images/background/InJail.jpg")));
		}
		return backgroundImage;
	}
	private JButton initRollDoubleButton() {
		if (rollDoubleButton == null) {
			rollDoubleButton = new JButton();
			rollDoubleButton.setText("Roll Double");
			rollDoubleButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					rollDoubleActionActionPerformed(event);
				}
			});
		}
		return rollDoubleButton;
	}

	private JButton initPostBailButton() {
		if (postBailButton == null) {
			postBailButton = new JButton();
			postBailButton.setText("Post Bail");
			postBailButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					postBailActionActionPerformed(event);
				}
			});
		}
		return postBailButton;
	}

	private JButton initUseCardButton() {
		if (useCardButton == null) {
			useCardButton = new JButton();
			useCardButton.setText("Use Card");
			if(null!=this.boardFrame.getCurrentPlayer().getDeed(28)||null!=this.boardFrame.getCurrentPlayer().getDeed(29)) {
				this.useCardButton.setEnabled(true);
			}
			else {
				this.useCardButton.setEnabled(false);
			}
			useCardButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					useCardActionActionPerformed(event);
				}
			});
		}
		return useCardButton;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				InJail dialog = new InJail();
				dialog.setDefaultCloseOperation(InJail.DISPOSE_ON_CLOSE);
				//dialog.setTitle("InJail");
				dialog.setLocationRelativeTo(null);
				dialog.getContentPane().setPreferredSize(dialog.getSize());
				dialog.pack();
				dialog.setVisible(true);
			}
		});
	}

	private void rollDoubleActionActionPerformed(ActionEvent event) {
		this.boardFrame.setTryForDoubles(true);
		this.dispose();
	}

	private void postBailActionActionPerformed(ActionEvent event) {
		this.boardFrame.getCurrentPlayer().postBail();
		this.boardFrame.getCurrentPlayer().setInJail(false);
		this.boardFrame.setJustOutJail(true);
		this.dispose();
	}

	private void useCardActionActionPerformed(ActionEvent event) {

		if(null!=this.boardFrame.getCurrentPlayer().getDeed(28)){
				this.boardFrame.getCurrentPlayer().setDeed(null, 28);
				this.boardFrame.getBoard().getContainer(7).getDeck().setGetOutJail(true);
			}
			else {
				this.boardFrame.getCurrentPlayer().setDeed(null, 29);
				this.boardFrame.getBoard().getContainer(2).getDeck().setGetOutJail(true);
			}
			this.boardFrame.getCurrentPlayer().setInJail(false);
			this.boardFrame.setJustOutJail(true);
			this.dispose();
	}

}
